package com.joe.common.tool.image;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
 * @email zhoulonghuan@gmail.com
 * @author joe
 * @since 2007-?
 */
public class Image2Base64AndRevert {
	
	public String imageToBase64() throws IOException{
		FileInputStream fs = new FileInputStream(new File("c:\\demoa.jpg"));
		
		/**方式1
		byte[] buf = new byte[1024];
		StringBuffer sb = new StringBuffer();
		BASE64Encoder encoder = new BASE64Encoder();
		while(fs.read(buf)!=-1){
			sb.append(encoder.encode(buf));
		}
		System.out.println(sb.toString());
		*/
		//方式2--用这种方式好点吧,我用上种方式,发现输出的encode后的字符串有些行比较长...
		byte[] buf = new byte[fs.available()];  //available方法返回还有多少字节可读,恰好作为数组的大小
		System.out.println(buf.length);
		fs.read(buf);
		//printHexString(buf);
		String sb = new sun.misc.BASE64Encoder().encode(buf);
		sb = sb.replaceAll("\r\n", "");
		System.out.println(sb);
		fs.close();
		return sb.toString();
	}
	
	public void base64ToImage(String s) throws IOException{
		FileOutputStream os = new FileOutputStream(new File("c:\\haha.jpg"));
		//c#中的Base64Decoder似乎能按指定大小长度读到byte[]里去,java中却不能,只能一次性读出来,所以也做
		//不了那样的循环写文件等等
		byte[] buf = new byte[new sun.misc.BASE64Decoder().decodeBuffer(s).length];
		System.out.println("buf.length: "+buf.length);
		buf = new sun.misc.BASE64Decoder().decodeBuffer(s);
		os.write(buf);
		os.flush();
		os.close();
	}
	// 将 s 进行 BASE64 编码
	public static String getBASE64(String s) {
		if (s == null)
			return null;
		return (new sun.misc.BASE64Encoder()).encode(s.getBytes());
	}

	// 将 BASE64 编码的字符串 s 进行解码
	public static String getFromBASE64(String s) {
		if (s == null)
			return null;
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			byte[] b = decoder.decodeBuffer(s);
			return new String(b);
		} catch (Exception e) {
			return null;
		}
	}
	
	public static void printHexString(byte[] b) {
		for (int i = 0; i < b.length; i++) {
			String hex = Integer.toHexString(b[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			System.out.print(hex.toUpperCase());
		}
//		StringBuffer sb = new StringBuffer("");
//		for (int i = 0; i < b.length; i++) {
//			String hex = Integer.toHexString(b[i] & 0xFF);
//			if (hex.length() == 1) {
//				hex = '0' + hex;
//			}
//			sb.append(hex.toUpperCase());
//		}
//		return sb.toString();
	}
   /**
	 * 存储图片16进制串为图片文件
	 * 
	 * @param src
	 *            图片16进制文本串
	 * @param output
	 *            要保存的图像文件路径
	 */
	public void saveToImgFile(String src, String output) {
		if (src == null || src.length() == 0) {
			return;
		}
		try {
			FileOutputStream out = new FileOutputStream(new File(output));
			byte[] bytes = src.getBytes();
			for (int i = 0; i < bytes.length; i += 2) {
				out.write(charToInt(bytes[i]) * 16 + charToInt(bytes[i + 1]));
			}
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private int charToInt(byte ch) {
		int val = 0;
		if (ch >= 0x30 && ch <= 0x39) {
			val = ch - 0x30;
		} else if (ch >= 0x41 && ch <= 0x46) {
			val = ch - 0x41 + 10;
		}
		return val;
	}

	public static void main(String[] args) throws IOException {
		Image2Base64AndRevert i = new Image2Base64AndRevert();
		//String s = i.imageToBase64();
		//System.out.println(s);
		//i.base64ToImage(s);
		
		String t = "/9j/4AAQSkZJRgABAQEAYABgAAD/4QBmRXhpZgAASUkqAAgAAAAEABoBBQABAAAAPgAAABsBBQAB"+
					"AAAARgAAACgBAwABAAAAAgAAADEBAgAQAAAATgAAAAAAAABgAAAAAQAAAGAAAAABAAAAUGFpbnQu"+
					"TkVUIHYzLjM2AP/bAEMAAgEBAQEBAgEBAQICAgICBAMCAgICBQQEAwQGBQYGBgUGBgYHCQgGBwkH"+
					"BgYICwgJCgoKCgoGCAsMCwoMCQoKCv/bAEMBAgICAgICBQMDBQoHBgcKCgoKCgoKCgoKCgoKCgoK"+
					"CgoKCgoKCgoKCgoKCgoKCgoKC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3LEUmrqSt6lOity28H69dH5NMuf+/Tf4V0mm/CnVrmRftL+QmRncvOPzq40pydkjlrZrg6KvOovvPP8V0vh7wXqmvTAxwMIQRuckDAPfmvWNE+Guj6V886rdyEc70wK7CC3htkVIIo40H8KKAP0rspYCT1nofMZhxZGzhhVfzZg+GvCFh4dtUEUSm4xhpck55z3rowMUtFenCEYLlifFVq9SvN1Kju2JS0UVZkFFFFABRRRQAUUUUAFGKKKIEwKNooooAY9vFJ9+NW+ozTVtLdTlYI1PqFAooqeVdiuaW1yXaPTH0owKKKokMUtFFABRRRQAUUUUAFFFFAH/9kgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIA==";
		String zmm1 = "==";
		//i.base64ToImage(zmm1);
		//i.imageToBase64();
		String hex = "FFD8FFE000104A46494600010101006000600000FFE1006645786966000049492A000800000004001A010500010000003E0000001B010500010000004600000028010300010000000200000031010200100000004E00000000000000600000000100000060000000010000005061696E742E4E45542076332E333600FFDB0043000201010101010201010102020202020403020202020504040304060506060605060606070908060709070606080B08090A0A0A0A0A06080B0C0B0A0C090A0A0AFFDB004301020202020202050303050A0706070A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0AFFC00011080005000503012200021101031101FFC4001F0000010501010101010100000000000000000102030405060708090A0BFFC400B5100002010303020403050504040000017D01020300041105122131410613516107227114328191A1082342B1C11552D1F02433627282090A161718191A25262728292A3435363738393A434445464748494A535455565758595A636465666768696A737475767778797A838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE1E2E3E4E5E6E7E8E9EAF1F2F3F4F5F6F7F8F9FAFFC4001F0100030101010101010101010000000000000102030405060708090A0BFFC400B51100020102040403040705040400010277000102031104052131061241510761711322328108144291A1B1C109233352F0156272D10A162434E125F11718191A262728292A35363738393A434445464748494A535455565758595A636465666768696A737475767778797A82838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE2E3E4E5E6E7E8E9EAF2F3F4F5F6F7F8F9FAFFDA000C03010002110311003F00F9D3E27FC52F0178CBC09E0BF047827E09693E1A7F0CE95245AD6B50DD3DC5EF882F6575692E2790850B1808A238403E5EE93E760C0294515FCA752ACEACF9A5BE9D12D95968BD0FF7B70782C3E0287B1A37E5BC9EB2949DE52727AC9B7BB7D6C968AC9247FFD9";
		//String afterhex = i.getBASE64(hex);
		//System.out.println(afterhex);
		//System.out.println(new sun.misc.BASE64Encoder().encode("中国人".getBytes()));
		//System.out.println(new sun.misc.BASE64Encoder().encode("中国人".getBytes("UTF-8")));
		i.imageToBase64();
	}
	
	/**固定长度的byte[]
		public String imageToBase64() throws IOException{
		FileInputStream fs = new FileInputStream(new File("c:\\demo.jpg"));
		byte[] buf = new byte[1024];
		fs.read(buf, 0, 1024);
		String s = new sun.misc.BASE64Encoder().encode(buf);
		System.out.println(s);
		
		return s;
	}
	
	public void base64ToImage(String s) throws IOException{
		FileOutputStream os = new FileOutputStream(new File("c:\\haha.jpg"));
		byte[] buf = new byte[1024];
		buf = new sun.misc.BASE64Decoder().decodeBuffer(s);
		os.write(buf, 0, 1024);
	}
	
	public static void main(String[] args) throws IOException {
		Image2Base64AndRevert i = new Image2Base64AndRevert();
		String s = i.imageToBase64();
		i.base64ToImage(s);
	}
	 */
	
	/**
	try {
        // Convert a byte array to base64 string
        byte[] buf = new byte[]{0x12, 0x23};
        String s = new sun.misc.BASE64Encoder().encode(buf);
    
        // Convert base64 string to a byte array
        buf = new sun.misc.BASE64Decoder().decodeBuffer(s);
    } catch (IOException e) {
    }
	 */

}
